home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 8: LINUX Games / Linux Cubed Series 8 - LINUX Games.iso / games / torus-sr.tar / torus-sr / torus / score.c < prev    next >
C/C++ Source or Header  |  1992-03-05  |  5KB  |  246 lines

  1. # include "robots.h"
  2.  
  3. /*
  4.  * score.c: All the scoring code is in here.
  5.  */
  6.  
  7. struct scorefile {
  8.     int    s_uid;
  9.     long    s_score;
  10.     char    s_name[MAXSTR];
  11.     bool    s_eaten;
  12.     int    s_level;
  13.     bool    s_hsew;
  14.     bool    s_vsew;
  15.     bool    s_wimpy;
  16.     int    s_days;
  17. };
  18.  
  19. # define FILE_SIZE    (NUMSCORES*sizeof(struct scorefile))
  20.  
  21. scoring(eaten)
  22.     bool eaten;
  23. {
  24.     static char buf[MAXSTR];
  25.         toral = hsew && vsew;
  26.     (void) sprintf(buf,"for this %s",TEMP_NAME);
  27.     if( record_score(eaten,toral?T_TMP_FILE:TMP_FILE,TEMP_DAYS,buf)
  28.             || show_highscore) {
  29.         printf("[Press return to continue]");
  30.         fflush(stdout);
  31.         gets(buf);
  32.     }
  33.     record_score(eaten,toral?T_HOF_FILE:HOF_FILE,0,"of All Time");
  34. }
  35.  
  36. # define    sigbit(x)    (1 << ((x) - 1))
  37.  
  38. record_score(eaten,fname,max_days,type_str)
  39.     bool eaten;
  40.     char *fname;
  41.     int max_days;
  42.     char *type_str;
  43. {
  44.     int value;
  45.     int fd;
  46.     int omask;
  47.  
  48.     /* block signals while recording the score
  49.      * hope this routine doesn't get stuck!
  50.      */
  51. # ifndef BSD42
  52. # ifdef TURBOC /* rfs */
  53.     void
  54. #elif linux
  55.         void
  56. # else
  57.     int
  58. # endif
  59.          (*oint)(), (*oterm)(), (*ohup)();
  60.  
  61.     oint = signal(SIGINT, SIG_IGN);
  62.     oterm = signal(SIGTERM, SIG_IGN);
  63. # ifndef TURBOC /* rfs */
  64.     ohup = signal(SIGHUP, SIG_IGN);
  65. # endif
  66. # else
  67.     omask = sigblock( sigbit(SIGINT) | sigbit(SIGTERM) | sigbit(SIGHUP)
  68.             | sigbit(SIGTSTP));
  69. # endif
  70.  
  71.     if((fd = lk_open(fname,
  72. # ifdef TURBOC /* rfs */
  73.                 O_RDWR | O_CREAT | O_BINARY
  74. # else
  75.                 2
  76. # endif
  77.                     )) < 0) {
  78.         perror(fname);
  79.     } else {
  80.         value = do_score(eaten,fd,max_days,type_str);
  81.         lk_close(fd, fname);
  82.     }
  83. # ifdef BSD42
  84.     (void) sigsetmask(omask);
  85. # else
  86.     (void) signal(SIGINT, oint);
  87.     (void) signal(SIGTERM, oterm);
  88. # ifndef TURBOC /* rfs */
  89.     (void) signal(SIGHUP, ohup);
  90. # endif
  91. # endif
  92.     return value;
  93. }
  94.  
  95. do_score(eaten,fd,max_days,type_str)
  96.     bool eaten;
  97.     int fd, max_days;
  98.     char *type_str;
  99. {
  100.     register struct scorefile *position;
  101.     register int x;
  102. /*    register struct scorefile *remove, *sfile, *eof; */ /* rfs */
  103.     register struct scorefile *remove, *eof; /* rfs */
  104.     static struct scorefile *sfile=NULL; /* rfs */
  105.     struct scorefile *oldest, *this;
  106.     int uid, this_day, limit;
  107.  
  108.     this_day = max_days ? time((time_t *)0)/SECSPERDAY : 0;
  109.     limit = this_day-max_days;
  110.     if (sfile==NULL) /* rfs */
  111.     sfile = (struct scorefile *)(malloc(FILE_SIZE));
  112.     if( sfile == NULL)
  113.     {
  114.         fprintf( stderr, "Out of memory so no scoring");
  115.         return FALSE;
  116.     }
  117.     eof = &sfile[NUMSCORES];
  118.     this = 0;
  119.     for(position = sfile; position < eof; position++) {
  120.         position->s_score = 0;
  121.         position->s_days = 0;
  122.     }
  123.     read(fd, (char *)sfile,FILE_SIZE);
  124.     remove = 0;
  125.     if(score > 0) {
  126.         uid = getuid();
  127.         oldest = 0;
  128.         x = limit;
  129.         for(position = eof-1; position >= sfile; position--) {
  130.             if(position->s_days < x) {
  131.                 x = position->s_days;
  132.                 oldest = position;
  133.             }
  134.         }
  135.         position = 0;
  136.         for(remove = sfile; remove < eof; remove++) {
  137.             if(position == 0 && score > remove->s_score) position = remove;
  138. # ifndef ALLSCORES
  139.             if(remove->s_uid == uid) break;
  140. # endif ALLSCORES
  141.         }
  142.         if(remove < eof) {
  143.             if(position == 0 && remove->s_days < limit) position = remove;
  144.         } else if(oldest) {
  145.             remove = oldest;
  146.             if(position == 0) {
  147.                 position = eof-1;
  148.             } else if(remove < position) {
  149.                 position--;
  150.             }
  151.         } else if(position) {
  152.             remove = eof-1;
  153.         }
  154.         if(position) {
  155.             if(remove < position) {
  156.                 while(remove < position) {
  157.                     *remove = *(remove+1);
  158.                     remove++;
  159.                 }
  160.             } else {
  161.                 while(remove > position) {
  162.                     *remove = *(remove-1);
  163.                     remove--;
  164.                 }
  165.             }
  166.             position->s_score = score;
  167.             (void) strncpy(position->s_name,whoami,MAXSTR);
  168.             position->s_eaten = eaten;
  169.             position->s_level = LEVEL;
  170.             position->s_uid = uid;
  171.             position->s_hsew = hsew;
  172.             position->s_vsew = vsew;
  173.             position->s_wimpy = wimpy;
  174.             position->s_days = this_day;
  175.             this = position;
  176.             if(lseek(fd,0L,0) == -1L ||
  177.                 write(fd,(char *)sfile,FILE_SIZE) != FILE_SIZE)
  178.                 perror("scorefile");
  179.             close(fd);
  180.         }
  181.     }
  182.     if( show_highscore || this )
  183.     {
  184.         printf(
  185. # ifdef ALLSCORES
  186.             "\nTop %s %s Scores %s:\n",
  187. # else ALLSCORES
  188.             "\nTop %s %s Robotists %s:\n",
  189. # endif ALLSCORES
  190.             NUMNAME,
  191.                         toral?"Toral":"Non-Toral",
  192.             type_str
  193.         );
  194.         printf("Rank     Score    Name\n");
  195.         for(position = sfile; position < eof; position++) {
  196.             if(position->s_score == 0) break;
  197.             if(position == this)
  198.                 putchar('>');
  199.             else  putchar(' ');
  200.             printf(
  201.                    "%c%2d %10ld  %8s: %s on %6s",
  202.                    position->s_days < limit ? '*' : ' ',
  203.                    position-sfile+1,
  204.                    position->s_score,
  205.                    position->s_name,
  206.                    position->s_eaten ?
  207.                      "robot food" : "ducked out",
  208.                    position->s_wimpy ? "wimpy" : "studly");
  209.             if(position->s_hsew)
  210.               printf( " %7s",
  211.                  (position->s_vsew) ? "toral" : "annular");
  212.             else
  213.               printf( " %7s",
  214.                  (position->s_vsew) ? "annular" : "planar");
  215.             printf(" level %d.", position->s_level);
  216.  
  217.             if(position == this)
  218.                 putchar('<');
  219.             putchar('\n');
  220.         }
  221.     }
  222.     return (this != 0);
  223. }
  224.  
  225. scorer()
  226. {
  227.     static char tels[6];
  228.     if(free_teleports != old_free) {
  229.         if(free_teleports > free_per_level) {
  230.             (void) sprintf(tels,"%d+%d",
  231.                        free_per_level,
  232.                        free_teleports-free_per_level);
  233.         } else {
  234.             (void) sprintf(tels,"%d",free_teleports);
  235.         }
  236.         old_free = free_teleports;
  237.     }
  238.     move(LINES-1,0);
  239.     clrtoeol();
  240.     printw("<%s> level: %d    score: %ld",tels,LEVEL,score);
  241.         mvprintw(LINES-1,RVPOS,"heaps:%3d robots:%3d value: %d",
  242.                 scrap_heaps,nrobots_alive,robot_value);
  243.     clrtoeol();
  244. }
  245.  
  246.